package com.vedeng.study.controller;

import com.comment.dto.CommentDTO;
import com.comment.dto.CommentRequest;
import com.vedeng.study.feign.BlogContentApi;
import com.vedeng.study.model.dto.BlogDTO;
import com.vedeng.study.model.dto.BlogDetailDTO;
import com.vedeng.study.request.BlogVO;
import com.vedeng.study.service.BlogServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Description:
 * @author Thresh.xie
 * @date 2021/12/13
 */
@RestController
public class TestController implements BlogContentApi {
    private static final Logger logger = LoggerFactory.getLogger(TestController.class);
    @Resource
    private BlogServiceImpl blogService;
    @Resource
    private RedisTemplate<String, String> redisTemplate;
    @Resource
    private RabbitTemplate rabbitTemplate;

    @GetMapping("test")
    public  String  test(){
      return "success";
    }

    @GetMapping("/")
    public String  method(){
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>(4);
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
        return "success";
    }




    @GetMapping("/sendTopicMessage1")
    public String sendTopicMessage1() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: man ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> manMap = new HashMap<>(4);
        manMap.put("messageId", messageId);
        manMap.put("messageData", messageData);
        manMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic.man", manMap);
        return "success";
    }

    @GetMapping("/sendTopicMessage2")
    public String sendTopicMessage2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: woman ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> womanMap = new HashMap<>(4);
        womanMap.put("messageId", messageId);
        womanMap.put("messageData", messageData);
        womanMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic.woman", womanMap);
        return "success";
    }



    @Override
    @RequestMapping("/blog/insertBlog")
    public int insertBlog(@RequestBody BlogVO blogVO){
        String blogTitle = blogVO.getBlogtitle();
        String blogAuthor = blogVO.getBlogauthor();
        String key = blogTitle+blogAuthor;
        String uuid = UUID.randomUUID().toString();

        Boolean lock = redisTemplate.opsForValue().setIfAbsent(key,uuid, 3, TimeUnit.SECONDS);
        if (ObjectUtils.isEmpty(lock)){
            return 0;
        }
        if (!lock) {
            return 0;
        }
        try {
            blogService.insert(blogVO);
        } finally {
            // 判断是否是当前线程的锁
            if (uuid.equals(redisTemplate.opsForValue().get(key))) {
                //释放锁
                redisTemplate.delete(key);
            }
        }
        return 1;
    }

    @Override
    @RequestMapping("/blog/deleteBlog")
    public int deleteBlog(BlogVO blogVO){
        blogService.delete(blogVO);
        return 1;
    }

    @Override
    @RequestMapping("/blog/updateBlog")
    public int updateBlog(BlogVO blogVO){
        blogService.update(blogVO);
        return 1;
    }

    @Override
    @RequestMapping("/blog/blogDetail")
    public BlogDetailDTO blogDetail(BlogVO blogVO){
        if (ObjectUtils.isEmpty(blogVO) || ObjectUtils.isEmpty(blogVO.getBlogid())){
            logger.warn("请求参数异常");
        }
        BlogDetailDTO detailDTO = new BlogDetailDTO();
        Long blogId = blogVO.getBlogid();
        blogVO = blogService.blogDetail(blogId);
        CommentRequest request = new CommentRequest();
        request.setBlogId(blogId);
        List<CommentDTO> commentDTOList = blogService.queryBlogComment(request);

        detailDTO.setBlogVO(blogVO);
        detailDTO.setCommentDTOList(commentDTOList);
        return detailDTO;
    }


    @Override
//    @RequestMapping("/blog/blogList")
    public List<BlogDTO> blogList( BlogVO blogVO){
        return blogService.queryBlogList(blogVO);
    }


    @Override
//    @RequestMapping("/blog/addComments")
    public String addComments(BlogVO blogVO){
        return blogService.addComments(blogVO);
    }



}
